var maxProduct = function (words) {
	let result = 0
	const checkItem = (a, b) => {
		let flag = false
		for (let i of b) {
			flag = flag || a.includes(i)
			if (flag) return true
		}
		return flag
	}
	for (let i = 0; i < words.length; i++) {
		for (let j = i + 1; j < words.length; j++) {
			if (!checkItem(words[i], words[j])) {
				result = Math.max(result, words[i].length * words[j].length)
			}
		}
	}
	return result
}


var integerReplacement = function (n) {
	const map = new Map([[1, 0]])
	function dfs (num) {
		if (map.has(num)) return map.get(num)
		const sm = 1 + dfs(Math.floor(num / 2));
		const mx = 1 + dfs(Math.ceil(num / 2));
		const result = (num % 2 ? 1 : 0) + Math.min(sm, mx)
		map.set(num, result)
		return result
	}
	return dfs(n)
};

var buddyStrings = function (s, goal) {
	if (s.length != goal.length) {
		return false;
	}

	if (s === goal) {
		const count = new Array(26).fill(0);
		for (let i = 0; i < s.length; i++) {
			count[s[i].charCodeAt() - 'a'.charCodeAt()]++;
			if (count[s[i].charCodeAt() - 'a'.charCodeAt()] > 1) {
				return true;
			}
		}
		return false;
	} else {
		let first = -1, second = -1;
		for (let i = 0; i < s.length; i++) {
			if (s[i] !== goal[i]) {
				if (first === -1)
					first = i;
				else if (second === -1)
					second = i;
				else
					return false;
			}
		}

		return (second !== -1 && s[first] === goal[second] && s[second] === goal[first]);
	}
};
/**
 * @param {string} s
 * @param {string} p
 * @return {number[]}
 */
var findAnagrams = function (s, p) {

	// 检查两个字符是不是异位字符
	function check (a, arr) {
		let sArr = new Array(26).fill(0)
		for (let i = 0; i < a.length; i++) {
			++sArr[a[i].charCodeAt() - 97]
		}
		return sArr.join('') === arr.join('')
	}
	// 循环检查
	let result = [],
		holdArr = new Array(26).fill(0),
		len = p.length
	for (let i = 0; i < len; i++) {
		++holdArr[p[i].charCodeAt() - 97]
	}
	for (let i = 0; i < s.length - len + 1; i++) {
		let str = s.slice(i, i + len)
		if (check(str, holdArr)) {
			result.push(i)
		}
	}
	return result
};

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var targetIndices = function (nums, target) {
	nums.sort((a, b) => a - b)
	const result = nums.map((i, index) => i === target ? index : undefined)
	console.log(nums, result);
	return result.filter(i => i !== undefined)
};

console.log(targetIndices([48, 90, 9, 21, 31, 35, 19, 69, 29, 52, 100, 54, 21, 86, 6, 45, 42, 5, 62, 77, 15, 38], 6));

